<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CateController extends CommonController
{
    // GET|HEAD admin/cate/create 创建分类
    public function create()
    {
        $category = Category::where('cate_pid','0')->get();
        return view('admin.category.add',compact('category'));
    }
    // POST admin/cate 入库
    public function store()
    {
        $input = Input::except('_token');
        // 验证
        $rules = array('cate_name'=>'required');
        $message = array('cate_name.required'=>'错误!分类名必须填写');
        $validator = Validator::make($input,$rules,$message);
       if($validator->passes()){
           // 验证通过入库
         $res = Category::create($input);
           if($res){
               return back()->with('errors','分类添加成功!');

           }else{
               return back()->with('errors','分类添加失败,请稍后再试!');
           }
       }else{
           return back()->withErrors($validator);
       }
    }

    // GET|HEAD admin/cate 全部列表
    public function index()
    {
        $category = Category::tree();
        return view('admin.category.index', compact('category'));
    }



    // DELETE admin/cate/{cate} 删除分类
    public function destroy($cate_id)
    {
        // 删除单个分类
       $res = Category::where('cate_id',$cate_id)->delete();
        // 将当前分类下的子分类移动到顶级分类下
        $re = Category::where('cate_pid',$cate_id)->update(['cate_pid'=>0]);

       if($res){
           $data = array('status'=>0,'msg'=>'分类删除成功!');
       } else{
           $data = array('status'=>1,'msg'=>'分类删除失败!');
       }
        return $data;
    }

    // PUT|PATCH admin/cate/{cate} 更新分类
    public function update($cate_id)
    {
        $input = Input::except('_token','_method');
        $field = Category::find($cate_id);
        $cate_pid = $field->cate_pid;
        $field->cate_pid = $input['cate_pid'];
        $res = $field->save();
        // 如果当前分类pid更新前后都是0 不能移动其子分类
        if($field->cate_pid !=0 || $input['cate_pid'] !=0 ){
            $re = Category::where('cate_pid',$cate_id)->update(['cate_pid'=>0]);
        }

        if($res){
            return redirect('admin/cate');
        }else{
            return back()->with('errors','分类信息更新失败!');
        }
    }

    // GET|HEAD admin/cate/{cate} 展示单个分类信息
    public function show()
    {
        echo 'show';
    }

    // GET|HEAD  admin/cate/{cate}/edit 编辑分类
    public function edit($cate_id)
    {
        $field = Category::find($cate_id);
        $category = Category::where('cate_pid','0')->get();
        return view('admin.category.edit',compact('category','field'));
    }

    // 修改分类排序
    public function changeOrder()
    {
        $input = Input::all();
        $field = Category::find($input['cate_id']);
        $field->cate_order = $input['cate_order'];
        $res = $field->update();
        $data = array();
        if($res){
          $data['status'] = 0;
          $data['msg'] = '排序修改成功!';
        }else{
           $data['status'] = 1;
           $data['msg'] = '排序修改失败,请稍后再试!';

        }
        return $data;

    }

    // ajax 检查分类 是否存在子分类
    public function categoryCheck (){
       $input = Input::except('_token');
       // 查询当前分类下面是否存在子分类
       $res = Category::where('cate_pid',$input['cate_id'])->get()->count();

       if($res){
          return array('status'=>1,'msg'=>'当前分类下存在子分类!如果要移动,<br>将会把其子分类移动到顶级分类下!');
       }else{
          return array('status'=>0);
       }
    }


}